<?php


namespace app\common\model;


use think\model\concern\SoftDelete;

class ShopsDepartment extends Base
{

    // 软删除
    use SoftDelete;
    protected $deleteTime = 'is_del';
    protected $defaultSoftDelete = 1;

    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_shops_department';
    // 主键ID
    protected $pk = 'department_id';

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    function get_list($field='*',$cond=null, $orderby='update_time asc')
    {
		$where = null;

        if(isset($cond['shop_id'])){
            $where[] = ['shop_id','=', $cond['shop_id']];
        }
        if(!empty($cond['is_print_all'])){
            $where[] = ['is_print_all','=', $cond['is_print_all']];
        }
//
//		if(!empty($cond['type'])){
//			$where[] = ['type','=', $cond['type']];
//		}
//
//		if(!empty($cond['type_name'])){
//			$where[] = ['type_name','LIKE', "%{$cond['type_name']}%"];
//		}

        $rs = $this
            ->field($field)
            ->where($where)
            ->order($orderby)
            ->select();
        return $rs;
    }

    // 获取分页列表
    function get_list_page($field,$cond, $orderby ='update_time desc',$limit)
    {
        $where = null;

        if(!empty($cond['shop_id'])){
            $where[] = ['d.shop_id','=', $cond['shop_id']];
        }
		if(!empty($cond['department_name'])){
			$where[] = ['department_name','LIKE', "%{$cond['department_name']}%"];
		}

        $rs = $this->field($field)
            ->alias('d')
            ->leftJoin('ddt_shops s','d.shop_id = s.shop_id')
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ["{$this->pk}" => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->{$this->pk};
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

    function Del($id)
    {
		$article = ShopsDepartment::get($id);
//		var_dump($article);
		return $article->delete();

    }
}